package bfs;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author wcj
 * @Date 2021/7/6 21:30
 * @Version 1.0
 */
public class RightView {

    /**
     * 右视图
     *
     * @param root
     * @return
     */
    public static List<Integer> bfs(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) {
            queue.add(root);
        }
        while (!queue.isEmpty()) {
            int len = queue.size();
            for (int i = 0; i < len; i++) {
                TreeNode tmp = queue.poll();
                if (tmp.left != null) {
                    queue.add(tmp.left);
                }
                if (tmp.right != null) {
                    queue.add(tmp.right);
                }
                if (i == len - 1) {
                    res.add(tmp.val);
                }
            }
        }
        return res;
    }
}
